﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;

namespace UI.Resources.Controls.Columns
{
	public class DataGridComboBoxSelectorColumn : DataGridTemplateColumn
	{
		public DataGridComboBoxSelectorColumn(string header, Binding valueBinding, Binding sourceListBinding)
		{
			var editingTemplate = new DataTemplate();
			var editor = new FrameworkElementFactory(typeof(ComboBox));
			editor.SetBinding(Selector.SelectedValueProperty, valueBinding);
			editor.SetBinding(ItemsControl.ItemsSourceProperty, sourceListBinding);
			editingTemplate.VisualTree = editor;
			editingTemplate.Seal();

			var textTemplate = new DataTemplate();
			var text = new FrameworkElementFactory(typeof(TextBlock));
			text.SetBinding(TextBlock.TextProperty, valueBinding);
			textTemplate.VisualTree = text;
			textTemplate.Seal();

			Header = header;
			CellTemplate = textTemplate;
			CellEditingTemplate = editingTemplate;
		}
	}
}